<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('获取授权码')" />
</head>
<body class="white-bg">
	<div class="col-xs-12 col-md-10 col-lg-8 col-md-offset-1  col-lg-offset-2">
		<form class="form-horizontal m" id="form-role-edit" >
			<input id="roleId" name="roleId" type="hidden"/>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">终端编码：</label>
				<div class="col-sm-8">
					<input class="form-control" th:value="${code}" name="code" id="code" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">终端机器码：</label>
				<div class="col-sm-8">
					<textarea rows="3" class="form-control" name="machineCode" id="machineCode" required>[[${machineCode}]]</textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">F码：</label>
				<div class="col-sm-8">
					<input class="form-control" name="exchangeCode" id="exchangeCode" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">是否用于关机软件：</label>
				<div class="col-sm-8">
                    <label class="checkbox-inline"><input type="checkbox" value="yes" name="shutdownOnly">是的，此授权码是用在关机软件授权</label>
				</div>
			</div>
			<div class="form-group" style="text-align: center;">
				<button class="btn btn-primary dim " onclick="submitHandler()" type="button">获取授权码</button>
				
				<button class="btn btn-info dim " onclick="clearForm()" type="button">清空</button>
			</div>
			<div style="padding: 20px">
				<div id="shouquanma"></div>
			</div>
			<div class="form-group" style="text-align: center;">
				<button class="btn btn-primary dim " onclick="textCopy()" type="button">复制授权码</button>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
	    $(function() {
			
		});
	    
	    function textCopy() {
	    	
	    	let t = $("#shouquanma").html();
	    	if(!t){
	    		$.modal.msgError("先要获取授权码~~~");
	    	}
	        // 如果当前浏览器版本不兼容navigator.clipboard
	        if (!navigator.clipboard) {
	            var ele = document.createElement("input");
	            ele.value = t;
	            document.body.appendChild(ele);
	            ele.select();
	            document.execCommand("copy");
	            document.body.removeChild(ele);
	            if (document.execCommand("copy")) {
	                console.log("复制成功！");
	            } else {
	                console.log("复制失败！");
	            }
	        } else {
	            navigator.clipboard.writeText(t).then(function () {
	                console.log("复制成功！");
	            }).catch(function () {
	                console.log("复制失败！");
	            })
	        }
	    }
	    
	    function clearForm(){
	    	$("input[name='code']").val("");
	        $("textarea[name='machineCode']").val("");
	        $("input[name='exchangeCode']").val("");
	        $("input[name='shutdownOnly']").prop('checked', false);
	    }
	     
		function submitHandler() {
	        if (!$.validate.form()) {
	        	return;
	        }
	        
	        var param = {};
	        param.code = $("input[name='code']").val();
	        param.machineCode = $("textarea[name='machineCode']").val();
	        param.exchangeCode = $("input[name='exchangeCode']").val();
	        param.shutdownOnly = $("input[name='shutdownOnly']").is(':checked') == true ? 1 : 0;
	        
	        var data = getAuth(param);
	    }
		
		function getAuth(param){
			
			$.ajax({
				type : "POST",
				url : "getAuth",
				data : {
					"code": param.code,
					"machineCode": param.machineCode,
					"exchangeCode": param.exchangeCode,
					"shutdownOnly": param.shutdownOnly
				},
				async : true,
				error : function(request) {
					$.modal.alertError("系统错误");
				},
				success : function(data) {
					$("#shouquanma").html("");
					$("input[name='exchangeCode']").val("");
					if(data.code == '0'){
						$("#shouquanma").html(data.sign);
					}else{
						$.modal.msgError(data.msg);
					}
				}
			});
		}
	</script>
</body>
</html>
